package com.vitabuybuy.tv.ui.mine.order.adapter

import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.library.common.util.DensityUtil
import com.library.common.widget.RecycleViewDivider
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.BuildConfig
import com.vitabuybuy.tv.databinding.ItemOrderStoreBinding
import com.vitabuybuy.tv.databinding.ItemWholeOrderBinding
import com.vitabuybuy.tv.dto.OrderDetailDto
import com.vitabuybuy.tv.dto.OrderListDto
import com.library.common.base.BindRecycAdapter
import com.vitabuybuy.tv.ui.mine.order.OrderListActivity
import com.vitabuybuy.tv.utils.StringUtil

class OrderListAdapter : BindRecycAdapter<ViewDataBinding, OrderListDto>() {

    private lateinit var itemOrderGoodAdapterA: ItemOrderGoodAdapterA
    private lateinit var itemOrderGoodAdapterB: ItemOrderGoodAdapterB

    override fun onBindViewHolder(
        mBinding: ViewDataBinding,
        itemData: OrderListDto,
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        when (itemData.status) {
            //待支付、已取消订单
            OrderListActivity.ALREADLY_CANCEL, OrderListActivity.WAITING_PAY -> {
                val binding = mBinding as ItemWholeOrderBinding
                binding.tvName.text = BuildConfig.APPNAME
                itemOrderGoodAdapterB = ItemOrderGoodAdapterB()
                binding.recyclerView.apply {
                    adapter = itemOrderGoodAdapterB
                    val manager = LinearLayoutManager(mContext)
                    manager.orientation = LinearLayoutManager.HORIZONTAL
                    layoutManager = manager
                }
                val itemList = ArrayList<OrderDetailDto.ItemListBean>()
                if (itemData.itemList.isNullOrEmpty()) {
                    for (child in itemData.childList) {
                        itemList.addAll(child.itemList)
                    }
                } else {
                    itemList.addAll(itemData.itemList)
                }
//                if(itemData.type == "bidding" || itemData.type == "pending"){
//                    itemList.addAll(itemData.itemList)
//                }else{
//                    for (child in itemData.childList) {
//                        itemList.addAll(child.itemList)
//                    }
//                }
                itemOrderGoodAdapterB.setNewData(itemList)
                itemOrderGoodAdapterB.setOnItemClickListener { _, _, _ ->
                    listener?.invoke(mBinding, itemData, position)
                }
                binding.tvTotal.text =
                    "共" + itemData.goodsQuantity + "件商品，合计 ￥" + StringUtil.to2Double(itemData.goodsAmount)
                binding.itemData = itemData
                if (itemData.status == OrderListActivity.ALREADLY_CANCEL) {
                    binding.btnDeleteOrder.visibility = View.VISIBLE
                    binding.btnCancelOrder.visibility = View.GONE
                    binding.btnPay.visibility = View.GONE
                } else {
                    binding.btnDeleteOrder.visibility = View.GONE
                    binding.btnCancelOrder.visibility = View.VISIBLE
                    binding.btnPay.visibility = View.VISIBLE
                }
                addClickA(binding, position)
            }
            //待发货  待收货  带评论
            else -> {
                val binding = mBinding as ItemOrderStoreBinding
                binding.tvTotal.text =
                    "共" + itemData.goodsQuantity + "件商品，合计 ￥" + StringUtil.to2Double(itemData.goodsAmount) + "含邮费￥0.00"
                binding.itemData = itemData
                itemOrderGoodAdapterA = ItemOrderGoodAdapterA()
                binding.recyclerView.apply {
                    adapter = itemOrderGoodAdapterA
                    layoutManager = LinearLayoutManager(mContext)
                    addItemDecoration(RecycleViewDivider(mContext, DensityUtil.dip2px(1f)))
                    setHasFixedSize(true)
                    isNestedScrollingEnabled = false
                }
                itemOrderGoodAdapterA.setNewData(itemData.itemList)
                itemOrderGoodAdapterA.setOnItemClickListener { _, _, _ ->
                    listener?.invoke(mBinding, itemData, position)
                }
                when (itemData.status) {
                    OrderListActivity.WAITING_SEND -> {
                        binding.btnRemindSend.visibility = View.VISIBLE
                        binding.btnCheckDeliver.visibility = View.GONE
                        binding.btnConfirmReceive.visibility = View.GONE
                        binding.btnBuyAgain.visibility = View.GONE
                        binding.btnDeleteOrder.visibility = View.GONE
                        binding.btnComment.visibility = View.GONE
                    }
                    OrderListActivity.WAITING_RECEIVER -> {
                        binding.btnRemindSend.visibility = View.GONE
                        binding.btnCheckDeliver.visibility = View.VISIBLE
                        binding.btnConfirmReceive.visibility = View.VISIBLE
                        binding.btnBuyAgain.visibility = View.GONE
                        binding.btnDeleteOrder.visibility = View.GONE
                        binding.btnComment.visibility = View.GONE
                    }
                    OrderListActivity.WAITING_COMMENT -> {
                        binding.btnRemindSend.visibility = View.GONE
                        binding.btnCheckDeliver.visibility = View.GONE
                        binding.btnConfirmReceive.visibility = View.GONE
                        binding.btnBuyAgain.visibility = View.VISIBLE
                        binding.btnDeleteOrder.visibility = View.VISIBLE
                        binding.btnComment.visibility = View.VISIBLE
                    }
                    OrderListActivity.ALREADLY_CLOSE -> {
                        binding.btnRemindSend.visibility = View.GONE
                        binding.btnCheckDeliver.visibility = View.GONE
                        binding.btnConfirmReceive.visibility = View.GONE
                        binding.btnBuyAgain.visibility = View.VISIBLE
                        binding.btnDeleteOrder.visibility = View.VISIBLE
                        binding.btnComment.visibility = View.GONE
                    }
                    else -> {
                        binding.btnRemindSend.visibility = View.GONE
                        binding.btnCheckDeliver.visibility = View.GONE
                        binding.btnConfirmReceive.visibility = View.GONE
                        binding.btnBuyAgain.visibility = View.GONE
                        binding.btnDeleteOrder.visibility = View.GONE
                        binding.btnComment.visibility = View.GONE
                    }
                }
                addClickB(binding, position)
            }
        }
    }


    private fun addClickA(binding: ItemWholeOrderBinding, position: Int) {
        binding.btnPay.setOnClickListener { onClickToPay(position) }
        binding.btnCancelOrder.setOnClickListener { onClickCancel(position) }
        binding.btnDeleteOrder.setOnClickListener { onClickDelete(position) }
    }

    private fun addClickB(binding: ItemOrderStoreBinding, position: Int) {
        binding.btnBuyAgain.setOnClickListener { onClickBuyAgain(position) }
        binding.btnCheckDeliver.setOnClickListener { onClickDeliver(position) }
        binding.btnComment.setOnClickListener { onClickComment(position) }
        binding.btnConfirmReceive.setOnClickListener { onClickConfirmReceive(position) }
        binding.btnDeleteOrder.setOnClickListener { onClickDelete(position) }
        binding.btnRemindSend.setOnClickListener { onClickReminder(position) }
    }

    override fun getItemViewType(position: Int): Int {
        if (mData[position].status == OrderListActivity.WAITING_PAY ||
            mData[position].status == OrderListActivity.ALREADLY_CANCEL
        ) {
            return 1001
        }
        return 1002
    }

    override fun normalViewId(viewType: Int): Int {
        if (viewType == 1001) {
            return R.layout.item_whole_order
        }
        return R.layout.item_order_store
    }


    lateinit var onClickToPay: (position: Int) -> Unit
    lateinit var onClickCancel: (position: Int) -> Unit
    lateinit var onClickReminder: (position: Int) -> Unit
    lateinit var onClickDelete: (position: Int) -> Unit
    lateinit var onClickBuyAgain: (position: Int) -> Unit
    lateinit var onClickDeliver: (position: Int) -> Unit
    lateinit var onClickConfirmReceive: (position: Int) -> Unit
    lateinit var onClickComment: (position: Int) -> Unit
}

